<?php
require_once("../data/db_connection.php");
require_once("../Utils.php");
require_once("../constants.php");
$db = new db();

$event_id = getFinalValue($_POST[PARAM_EVENT_ID]);
$event_state = getFinalValue($_POST[PARAM_EVENT_STATE]);

//$event_start = trim(getFinalValue($_POST[PARAM_EVENT_START]));
$event_start = trim($_POST[PARAM_EVENT_START]);
$event_start = substr($event_start, 0, 10);
$event_start = date("Y-m-d H:i:s", $event_start);

//$event_duration = trim(getFinalValue($_POST[PARAM_EVENT_DURATION]));
$event_duration = trim($_POST[PARAM_EVENT_DURATION]);
$event_duration = substr($event_duration, 0, 10);
$event_duration = date("Y-m-d H:i:s", $event_duration);

$event_location_id = getFinalValue($_POST[PARAM_EVENT_LOCATION_ID]);
$event_location_byname = getFinalValue($_POST[PARAM_EVENT_LOCATION_BYNAME]);
$event_owner_id = getFinalValue($_POST[PARAM_EVENT_OWNER_ID]);
$sql = "";
$return_id = 0;
// addpGetEventUnconfirmed
if ($event_id == 0) {
	$sql = "INSERT INTO pTableEventInfo values(NULL, " . $event_state . 
				", '" . $event_start . "', '" . $event_duration . "', " . 
				$event_location_id . ", '" .$event_location_byname . "', " . $event_owner_id .")";
	$return_id = $db->insert_get_id($sql); // n 或者 0
	if ($return_id) {
		// 并向用户信息表中插入一条，0 表示此活动是该用户创建
		$sql = "INSERT INTO pTableUserEvent VALUES(NULL, " . $user_id . 
					", ".$return_id .", 0)";
		$return_id = $db->insert_get_id($sql);
	}
} else {	// update
	$sql = "UPDATE pTableEventInfo Set pEventState = " . $event_state .
				", pEventStart = '" . $event_start . "', pEventDuration = '" .
				$event_duration . "', pEventLocationId = " . $event_location_id .
				", pEventLocationByname = '" . $evnet_location_byname ."', pEventOwnerId = " . $event_owner_id . " WHERE _id = " . $event_id;
	$return_id = $db->update_get_id($sql);	// 1 或者 0 或者 -1
}
echo $return_id;
?>